package main

import (
	"github.com/gin-gonic/gin"
)

func main() {
	r := gin.Default()
	r.GET("/cookie_v1", func(c *gin.Context) {
		cookie, err := c.Cookie("gin_cookie")
		if err != nil {
			cookie = "test"
			c.SetCookie("gin_cookie", "test", 3600*24*30, "/", "localhost", false, true)
		}
		c.JSON(200, gin.H{"cookie": cookie})
	})

	r.GET("/cookie_v2/:uid", cookie_v2)
	r.Run(":8080")
}

func cookie_v2(c *gin.Context) {
	//获取用户请求参数 用户uid
	uid := c.Param("uid")
	cookie, err := c.Cookie("user_id")
	if err != nil {
		cookie = uid
		c.SetCookie("user_id", uid, 3600*24*30, "/", "localhost", false, true)
	}
	c.JSON(200, gin.H{"cookie": cookie})
}
